//
//  LTLExpressionTool.m
//  WeiBo
//
//  Created by Apple_Lzzy46 on 16/11/10.
//  Copyright © 2016年 LTL. All rights reserved.
//

#import "LTLExpressionTool.h"

#import "LTLButton.h"

@interface LTLExpressionTool ()

@property(strong,nonatomic)LTLButton *button;
@end

@implementation LTLExpressionTool
-(instancetype)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self) {
        //创建按钮
        [self setupBtnWithTitle:@"最近"buttonType:LTLExpressionTypeLately];
        [self setupBtnWithTitle:@"默认"buttonType:LTLExpressionTypeDefault];
        [self setupBtnWithTitle:@"emoji"buttonType:LTLExpressionTypeEmoji];
        [self setupBtnWithTitle:@"浪小花"buttonType:LTLExpressionTypeLxh];
    }
    return self;
}
-(void)setupBtnWithTitle:(NSString *)title buttonType:(LTLExpressionType) tag
{
    LTLButton * btn =[[LTLButton alloc]init];
    [btn addTarget:self action:@selector(didButton:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:title forState:UIControlStateNormal];
    ///绑定 ID
    btn.tag =tag;
    
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    
    [self addSubview:btn];
    
    //设置默认状态
    if ([title isEqual:@"默认"]) {
        [self didButton:btn];
    }
    //加载背景图
    if (self.subviews.count == 1) {
        //常态图
        [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_left_normal"]forState:UIControlStateNormal];
        //高亮图
        [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_left_selected"]forState:UIControlStateSelected];
        
    }else if (self.subviews.count == 4)
    {
        //常态图
        [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_right_normal"]forState:UIControlStateNormal];
        //高亮图
        [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_right_selected"]forState:UIControlStateSelected];
        
    }else
    {
        //常态图
        [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_mid_normal"]forState:UIControlStateNormal];
        //高亮图
        [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_mid_selected"]forState:UIControlStateSelected];
        
    }
    
    
}
/**
 * 子控件布局
 */
-(void)layoutSubviews
{
    [super layoutSubviews];
    //设置所有按钮的frame
    NSUInteger count = self.subviews.count;
    CGFloat btnW = self.width /count;
    CGFloat btnH = self.highly;
    for (NSUInteger i =0; i<count; i++) {
        UIButton *btn =self.subviews[i];
        btn.x =i*btnW;
        btn.y =0;
        btn.width =btnW;
        btn.highly =btnH;
    }
    
}
//按钮点击事件
-(void)didButton:(LTLButton *)button
{
    //交换状态
    _button.selected = NO;
    button.selected = YES;
    _button = button;
    LTLExpressionType type = button.tag;
    
    if ([self.delegate respondsToSelector:@selector(LTLExpressionTool:didClickBtn:)]) {
        [self.delegate LTLExpressionTool:self didClickBtn:type];
    }
}
//当前表情
-(void)setType:(LTLExpressionType)Type
{
    _Type = Type;
    LTLButton *buttn = [self viewWithTag:Type];
    
    [self didBtm:buttn];
}
-(void)didBtm:(LTLButton *)button
{
    //交换状态
    _button.selected = NO;
    button.selected = YES;
    _button = button;


}

@end
